package compare;

import opencv.utils.InitUtils;
import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import xiugai.other.OpencvUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 国徽
 */
public class GuoHui_Sharp {
    static final String perfx ="sharp";
    static {
        InitUtils.addLibrary();
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        //注意程序运行的时候需要在VM option添加该行 指明opencv的dll文件所在路径
        //-Djava.library.path=$PROJECT_DIR$\opencv\x64
    }

    public static void main(String[] args) {
        String path="C:\\Users\\admin\\Desktop\\shengfenzheng\\OIP.jpg";
        Mat mat= Imgcodecs.imread(path);
        houghLinesP(mat);
    }

    public static List<MatOfPoint> houghLinesP(Mat mat) {
        mat= OpencvUtil.gray(mat);
        Mat kernel = new Mat(3, 3, CvType.CV_32F);
        float[] kernelData = new float[(int) (kernel.total() * kernel.channels())];
        kernelData[0] = 1; kernelData[1] = 1; kernelData[2] = 1;
        kernelData[3] = 1; kernelData[4] = -8; kernelData[5] = 1;
        kernelData[6] = 1; kernelData[7] = 1; kernelData[8] = 1;
        kernel.put(0, 0, kernelData);

        // do the laplacian filtering as it is
        // well, we need to convert everything in something more deeper then CV_8U
        // because the kernel has some negative values,
        // and we can expect in general to have a Laplacian image with negative values
        // BUT a 8bits unsigned int (the one we are working with) can contain values
        // from 0 to 255
        // so the possible negative number will be truncated
        Mat imgLaplacian = new Mat();
        Imgproc.filter2D(mat, imgLaplacian, CvType.CV_32F, kernel);
        Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/"+perfx+"_ruihua.jpg", mat);

        Mat kai = new Mat();
        //开运算
        kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(25,25));
        Imgproc.morphologyEx(mat, kai, Imgproc.MORPH_OPEN, kernel);


        //腐蚀
        mat = OpencvUtil.erode(kai, 4);
        mat = OpencvUtil.dilate(kai, 4);
        Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/"+perfx+"002.jpg", mat);
        Mat bi = new Mat();
        Imgproc.morphologyEx(mat, bi, Imgproc.MORPH_CLOSE, kernel);
        List<MatOfPoint> contours=new ArrayList<>();
        Mat hierarchy = new Mat();
        Imgproc.findContours(mat, contours, hierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
        Imgproc.contourArea(contours.get(0));
        Imgproc.drawContours(mat,contours,-1,new Scalar(0,0,255),1);
        Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/"+perfx+"003.jpg", mat);



//        //灰度
//        //二值化
//        mat = OpencvUtil.binary(mat);
////        Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/"+perfx+"_all_erzhihua.jpg", mat);
//        //腐蚀
//        Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/"+perfx+"_all_fushi.jpg", mat);
//        //边缘检测
//        mat = OpencvUtil.carry(mat);
//        Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/"+perfx+"_all_bianyuanjiance.jpg", mat);
//        //降噪
//        mat = OpencvUtil.navieRemoveNoise(mat, 1);
//        Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/"+perfx+"_all_jiangzhao.jpg", mat);
//        //膨胀
//        Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/"+perfx+"_all_pengzhang.jpg", mat);

        //轮廓检测,清除小的轮廓部分
        return contours;
    }
}
